package com.ruoyi.web.controller.netty.http.custom;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 错误请求统计和正确请求统计
 */

@Slf4j
@ChannelHandler.Sharable
public class HttpNettyCountHandler extends ChannelInboundHandlerAdapter {

    static AtomicInteger successCount = new AtomicInteger();
    static AtomicInteger errorCount = new AtomicInteger();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        FullHttpRequest httpRequest = (FullHttpRequest)msg;
        String uri = httpRequest.uri();
        if ("/test".equals(uri)){
            successCount.incrementAndGet();
        }else{
            errorCount.incrementAndGet();
        }
        log.info("请求成功数为：" + successCount);
        log.info("请求失败数为：" + errorCount);
        //传递给下一个handler
        ctx.fireChannelRead(msg);
    }
}
